package com.atguigu.eduservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.openfeign.VodClient;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ZhanK
 * @since 2022-02-20
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    //通过FeignClient调用删除视频服务的接口，实现远程调用
    @Autowired
    VodClient vodClient;
    @Override
    public R removeByVideoId(String eduVideoId) {
        //通过Feign远程接口实现，自动注入其他模块的接口调用其他模块的功能
        //根据小节id查询出视频id，进行删除
        EduVideo video = baseMapper.selectById(eduVideoId);
        String videoSourceId = video.getVideoSourceId();
        //判断是否有视频,有就删除
        if(!StringUtils.isEmpty(videoSourceId)){
            //远程调用vod删除视频
            R r = vodClient.removeVideo(videoSourceId);
            System.out.println("=============="+r);
            if (r.getCode()==20001){ //这个是调用了消费者的服务降级，因为没收到vod服务器的响应

                throw new GuliException(20001,r.getMessage());
            }
            if (r.getCode()==20002){ //这个是收到了vod服务自己降级后的返回值，vod自身服务降级后传给edu

                throw new GuliException(20001,r.getMessage());
            }

            baseMapper.deleteById(eduVideoId);

            return R.ok();
        }

        return R.error().message("没找到该小节的名称，删除失败！");

    }
}
